<template>
  <div class="admin-login-container">
    <div class="admin-login-card">
      <div class="card-header">
        <div class="logo">
          <i class="el-icon-s-tools"></i>
        </div>
        <h2>管理员登录</h2>
        <p>欢迎回来，请登录您的管理员账户</p>
      </div>
      
      <div class="card-body">
        <el-form 
          ref="loginForm" 
          :model="form" 
          :rules="rules" 
          label-width="0"
          @submit.prevent="handleLogin"
        >
          <el-form-item prop="username">
            <el-input 
              v-model="form.username" 
              placeholder="请输入用户名"
              prefix-icon="el-icon-user"
              size="large"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input 
              v-model="form.password" 
              type="password" 
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              size="large"
              show-password
              @keyup.enter.native="handleLogin"
            />
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="primary" 
              :loading="loading" 
              @click="handleLogin"
              class="login-btn"
              size="large"
            >
              {{ loading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="footer-links">
          <router-link to="/init" class="init-link">
            <i class="el-icon-setting"></i>
            初始化管理员
          </router-link>
          <router-link to="/login" class="back-link">
            <i class="el-icon-back"></i>
            返回用户登录
          </router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import axios from 'axios'

export default {
  name: 'AdminLogin',
  data() {
    return {
      loading: false,
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ...mapActions(['login']),
    
    async handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          this.loading = true
          try {
            const response = await axios.post('/api/admin/admin-login', {
              username: this.form.username,
              password: this.form.password
            })
            
            if (response.data.success) {
              const adminInfo = response.data.data
              
              // 设置管理员角色标识
              adminInfo.userRole = 'ADMIN'
              adminInfo.role = 'ADMIN'
              
              // 使用Vuex store保存管理员信息
              const result = await this.login({
                userInfo: adminInfo,
                context: 'admin'
              })
              
              if (result.success) {
                this.$message.success('登录成功！')
                // 跳转到管理后台
                this.$router.push('/admin-panel/dashboard')
              } else {
                this.$message.error(result.message || '登录失败')
              }
            } else {
              this.$message.error(response.data.message || '登录失败')
            }
          } catch (error) {
            console.error('登录失败:', error)
            this.$message.error(error.response?.data?.message || '登录失败，请检查用户名和密码')
          } finally {
            this.loading = false
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.admin-login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #409eff 0%, #667eea 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.admin-login-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  width: 100%;
  max-width: 400px;
}

.card-header {
  background: linear-gradient(135deg, #409eff 0%, #667eea 100%);
  color: white;
  padding: 40px 30px;
  text-align: center;
}

.logo {
  font-size: 48px;
  margin-bottom: 20px;
  opacity: 0.9;
}

.card-header h2 {
  margin: 0 0 10px 0;
  font-size: 24px;
  font-weight: 600;
}

.card-header p {
  margin: 0;
  opacity: 0.9;
  font-size: 14px;
}

.card-body {
  padding: 40px 30px;
}

.el-form-item {
  margin-bottom: 25px;
}

.el-input--large .el-input__inner {
  height: 50px;
  line-height: 50px;
  border-radius: 8px;
  border: 1px solid #e0e6ed;
  font-size: 14px;
  transition: all 0.3s ease;
}

.el-input--large .el-input__inner:focus {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

.login-btn {
  width: 100%;
  height: 50px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #409eff 0%, #667eea 100%);
  border: none;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(64, 158, 255, 0.3);
}

.footer-links {
  display: flex;
  justify-content: space-between;
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #f0f0f0;
}

.init-link,
.back-link {
  color: #666;
  text-decoration: none;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 5px;
  transition: color 0.3s ease;
}

.init-link:hover,
.back-link:hover {
  color: #409eff;
}

.init-link i,
.back-link i {
  font-size: 16px;
}
</style> 